using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;
using Telerik.Web.UI.Editor.DialogControls;
using MCWEBSITE.ENTITY;
using MCWEBSITE.BUL;
    //protected void GetNewsThumbParameter()
    //{
    //    lblResourceDir.Text = (string)Request.QueryString["ResourceDir"];
    //    lblNewsType.Text = (string)Request.QueryString["Act"];
    //}
public partial class Modules_WebsiteBackEnd_wucResourceUpload : System.Web.UI.UserControl
{
    private DataSet ImageDataset
    {
        get { return (DataSet)ViewState["ImagesDataset"]; }
        set { ViewState["ImagesDataset"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.GetNewsThumbParameter();
            if (rulImage.TargetFolder == "")
            {
                this.SetUploadParameters(lblResourceDir.Text);
                this.BindThumbViewerList();
            }
        }
    }

    //Get Thumb Parameter from Request Page
    protected void GetNewsThumbParameter()
    {
        //lblNewsType.Text = (string)Request.QueryString["NewsType"];
        //lblResourceDir.Text = (string)Request.QueryString["ResourceDir"];
        //lblImageThumbDir.Text = (string)Request.QueryString["ThumbDir"];
        lblResourceDir.Text = (string)Request.QueryString["ResourceDir"];
        lblNewsType.Text = (string)Request.QueryString["Act"];
    }

    //SET PARAMETERS FOR RAD UPLOAD
    public void SetUploadParameters(string ResourceDirectory)
    {
        rulImage.TargetFolder = ResourceDirectory;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ShowImage(object sender, RadRotatorEventArgs e)
    {
        DataSet ImageDataset = this.ImageDataset;
        imgThumbViewer.ImageUrl = Convert.ToString(ImageDataset.Tables[0].Rows[e.Item.Index][1]);
    }

    /// <summary>
    /// PREVIEW THUMB IN THUMB DIRECTORY 
    /// </summary>
    public void BindThumbViewerList()
    {
        if (!lblResourceDir.Text.Equals(""))
        {
            // Create dataset
            DataSet objThumbDS = new DataSet("ThumbDataset");
            // Create table
            DataTable objThumbTable = new DataTable("ThumbImages");
            objThumbTable.Columns.Add("ThumbName", System.Type.GetType("System.String"));
            objThumbTable.Columns.Add("ThumbUrl", System.Type.GetType("System.String"));
            objThumbDS.Tables.Add(objThumbTable);
            // Add row  
            DirectoryInfo ThumbDir = new DirectoryInfo(Server.MapPath(lblResourceDir.Text));
            foreach (FileInfo file in ThumbDir.GetFiles())
            {
                DataRow objRow = objThumbTable.NewRow();
                objRow[0] = file.Name;
                objRow[1] = lblResourceDir.Text + "/" + file.Name;
                objThumbTable.Rows.Add(objRow);
            }
            objThumbDS.AcceptChanges();
            this.ImageDataset = objThumbDS;
            rrttrThumbList.DataSource = objThumbDS;
            rrttrThumbList.DataBind();
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (rulImage.UploadedFiles.Count > 0)
        {
            // Get recent uploaded thumb
            string UploadedFilePath = "";
            UploadedFile FirstUploadedFile = rulImage.UploadedFiles[0];
            UploadedFilePath = Server.MapPath(lblResourceDir.Text + "/" + FirstUploadedFile.GetName());

            // Delete thumbimg directory
            DirectoryInfo ThumbDirectory = new DirectoryInfo(Server.MapPath(lblResourceDir.Text));
            if (ThumbDirectory.Exists == false)
            {
                ThumbDirectory.Create();
            }
            //    foreach (FileInfo file in ThumbDirectory.GetFiles())
            //        file.Delete();
            // Resize to all specific size based on key setting and save to thumbdirectory        
            //string[] ThumbSetting = System.Web.Configuration.WebConfigurationManager.AppSettings["NEWSTYPE-" + lblNewsType.Text].Split(';');
            string[] ThumbSetting = System.Web.Configuration.WebConfigurationManager.AppSettings["THUMB-" + rcbxThumbSize.SelectedItem.Text.ToString().Trim()].Split(';');
            FileInfo FileUploaded = new FileInfo(UploadedFilePath);
            if (FileUploaded.Exists)
            {
                for (int i = 0; i < ThumbSetting.Length; i++)
                {
                    string x = ThumbSetting[i].Split('x')[0];
                    string y = ThumbSetting[i].Split('x')[1];
                    ImageHelperExt.MAX_THUMBNAIL_WIDTH = Convert.ToInt32(x);
                    ImageHelperExt.MAX_THUMBNAIL_HEIGHT = Convert.ToInt32(y);
                    if (ThumbDirectory.Exists)
                    {
                        foreach (FileInfo file in ThumbDirectory.GetFiles())
                        {
                            if (file.Name.ToString() == "thumb" + ThumbSetting[i] + ".jpg")
                                file.Delete();
                            if (file.Name.ToString() == "Thumbs.db")
                                file.Delete();
                        }
                    }
                    ImageHelperExt.CreateThumb(UploadedFilePath, Server.MapPath(lblResourceDir.Text) + "/thumb" + ThumbSetting[i] + ".jpg"); //FirstUploadedFile.GetExtension()
                    // Utilities.ResizeNewImageBestFit(UploadedFilePath, Server.MapPath(lblImageThumbDir.Text), "thumb" + ThumbSetting[i] + FirstUploadedFile.GetExtension(), Convert.ToInt32(x), Convert.ToInt32(y));
                }
            }
            //Bind Thumb Created to view
            this.BindThumbViewerList();
        }
    }
}
